package com.study.volatile_study;

/**
 * Created by hgg on 2019/12/4.
 */
public class StaticVolatileTest {

    private static int num = 0;
    private static ThreadLocal<Integer> tl = new ThreadLocal<>();
//    private static volatile int num = 0;
    static Object lock = new Object() ;

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    String ss = num+" ";
                    num = (int) (Math.random()*100);
                    ss += num;
                    System.out.println(Thread.currentThread().getName()+" || "+ss);
                }
            }
        };

        for (int i=1;i<=100;i++){
            new Thread(runnable,"t"+i).start();
        }



        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {

                String ss = tl.get()+" ";
                Integer tmpNum = (int) (Math.random()*100);
                tl.set(tmpNum);
                tl.set(123);
                ss += tl.get();
                System.out.println(Thread.currentThread().getName()+" || "+ss);
            }
        };

        for (int i=1;i<=100;i++){
            new Thread(runnable2,"|||||t"+i).start();
        }
    }
}
